package com.itheima.service.impl;

import com.itheima.anno.MyBj;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptLogService;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Primary
public class DeptServiceImpl2 implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpService empService;
    @Autowired
    private DeptLogService deptLogService;

    @MyBj
    @Override
    public List<Dept> selectAll() {
        //int i=10/0;
        return deptMapper.selectAll();
    }

    @MyBj
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delById(Integer deptId) {
        int i = deptMapper.delById(deptId);
        int i2=empService.delByDeptId(deptId);
        deptLogService.insert(new DeptLog(null,LocalDateTime.now(),
                i+","+i2));
        int a=1/0;

        return i;
    }

    @Override
    public int insertDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.insertDept(dept);
    }

    @Override
    public Dept selectById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public int updateDept(Dept dept) {
        return deptMapper.updateDept(dept);
    }
}
